Softmax

Implement softmax and investigate its properties.


In [26]:
%matplotlib inline

import numpy as np

scores = np.array([1.0, 2.0, 3.0])

def softmax(x):
    """Compute softmax values for each sets of scores in x."""
    return np.exp(x) / np.sum(np.exp(x), axis=0)

print(softmax(scores))

# Plot softmax curves
import matplotlib.pyplot as plt
x = np.arange(-2.0, 6.0, 0.1)
scores = np.vstack([x, np.ones_like(x), 0.2 * np.ones_like(x)])

plt.plot(x, softmax(scores).T, linewidth=2)
plt.show()


[ 0.09003057  0.24472847  0.66524096]

Cross entropy

A visualization of how cross entropy changes.


In [27]:
# Cross entropy
x = np.arange(0, 1, 0.05)
plt.plot(x, -np.log(x))


/usr/local/lib/python3.5/site-packages/ipykernel/__main__.py:3: RuntimeWarning: divide by zero encountered in log
  app.launch_new_instance()
Out[27]:
[<matplotlib.lines.Line2D at 0x10a3de2b0>]

Example of numerical instability


In [29]:
x = 1000000000
for i in range(1000000):
    x += 0.000001
print(x - 1000000000)


0.95367431640625

Assignment 1